﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFExcelReport
{
    public partial class SXNVK02
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public DataTable TBPRG
        {
            get
            {
                return app.TBPRG;
            }
            set
            {
                app.TBPRG = value;
            }
        }
        public DataTable TBUR
        {
            get
            {
                return app.TBUR;
            }
            set
            {
                app.TBUR = value;
            }
        }
        VSXNVK02 vm;
        public SXNVK02()
        {

            vm = new VSXNVK02();
            DataContext = vm;
            InitializeComponent();
            this.Name = "销售统计";
            cVK02.LFD = "000011";
            IsCanNew = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            SetUpUserRight();
            IsLoaded = true;
        }
        private void btnCharting_Click(object sender, RoutedEventArgs e)
        {
            SetUpChart();
        }
        private void SetUpChart()
        {
            try
            {
                DataTable tb = cVK02.TBMain;
                DataTable data;
                string name = "2013";
                string X = "KDKUNDNA";
                string Y = "VKPPOSWERT";
                string lastmonth = "";
                string begin = "", end = "";
                end = Helpers.Helper.GetLastDayOfMonth(System.Convert.ToInt16(vm.year), System.Convert.ToInt16(vm.month));
                string filter = "VKPNEUDAT >=" + vm.year + vm.month + "01" + " and VKPNEUDAT<= " + end;
                DataRow[] rows = tb.Select(filter);
                if (rows.Length > 0)
                {
                    name = vm.month;
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    
                    data = rows.CopyToDataTable();
                    foreach (DataRow row in data.Rows)
                    {
                        row["KDKUNDNA"] = Helpers.Helper.GetGB2312Value(row["KDKUNDNA"].ToString());
                    }
                    Chart04.SetUp(1, name, data, X, Y);
                }
                Helpers.Helper.GetLastMonth(vm.date.ToString(), ref lastmonth, ref begin, ref end);
                filter = "VKPNEUDAT >=" + begin + " and VKPNEUDAT<=" + end;
                rows = tb.Select(filter);
                if (rows.Length > 0)
                {
                    data = rows.CopyToDataTable();
                    foreach (DataRow row in data.Rows)
                    {
                        row["KDKUNDNA"] = Helpers.Helper.GetGB2312Value(row["KDKUNDNA"].ToString());
                    }
                    name = lastmonth.Substring(4,2);
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    Chart04.SetUp(2, name, data, X, Y);
                }
                lastmonth = begin;
                Helpers.Helper.GetLastMonth(vm.date.ToString(), ref lastmonth, ref begin, ref end);
                filter = "VKPNEUDAT >=" + begin + " and VKPNEUDAT<=" + end;
                rows = tb.Select(filter);
                if (rows.Length > 0)
                {
                    data = rows.CopyToDataTable();
                    foreach (DataRow row in data.Rows)
                    {
                        row["KDKUNDNA"] = Helpers.Helper.GetGB2312Value(row["KDKUNDNA"].ToString());
                    }
                    name = lastmonth.Substring(4, 2);
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    Chart04.SetUp(3, name, data, X, Y);
                }
                Chart04.Confirm();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void SetUpUserRight()
        {
            base.SetUpUserRight();
            IsCanNew = false;
            Type mom = this.GetType();
            if (mom == null)
                return;
            string filter = "USERID='" + app.User + "' AND PROG='" + mom.Name.PadRight(80) + "'";
            DataRow[] rows = TBUR.Select(filter);
            foreach (DataRow row in rows)
            {
                string contol = row["CONTROLS"].ToString();//属性 
                string PROCESS = row["PROCESS"].ToString();//控制值
                string txt = row["P0"].ToString();//文本值 
                bool blControl = true;
                if (PROCESS.Trim() == "N")
                {
                    blControl = false; ;
                }
                if (PROCESS.Trim() == "Y")
                {
                    blControl = true; ;
                }
                switch (contol.Trim())
                {
                    case "DEL":
                        IsCanDelete = blControl;
                        break;
                    case "NEW":
                        IsCanNew = blControl;
                        break;
                    case "CHG":
                        IsCanChg = blControl;
                        break;
                    case "COPY":
                        IsCanCopy = blControl;
                        break;
                    case "MS":
                        ScopeManage = txt;
                        break;
                }
            }
        }

    }
}
